/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MBAPP_MODULE_
#include "mbapp.h"
#include "osObjects.h"                                          // RTOS object definitions

uint16_t DataRecevie[32];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;
uint32_t mbCnt;
uint32_t TickTimeCnt;
extern uint32_t uwTick;

void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt);
void mbsimulink(char mbid);

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/    
void mbTask(void const *argument)
{
    uint32_t MbCnt=0;
    
    mbyInit(MB_RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbyObj.SlaveId=2;
    mbzInit(MB_RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbzObj.SlaveId=2;
    
    RegHolding[0].uVal = 1;
    RegHolding[1].uVal = 1;
    RegHolding[2].uVal = 1;
    RegHolding[14].uVal = 1;
    RegHolding[15].uVal = 1;
    TickTimeCnt = uwTick;
    
    /*
    if(mbzObj.RunSt.bits.MasterMode)
    {
        mbStaticCmdSet(&mbzObj, 0, 2, FUN_CODE_READ_REG, DataRecevie, 0x10, 8, 0);
        mbStaticCmdSet(&mbzObj, 1, 2, FUN_CODE_READ_REG, DataRecevie, 0x10, 6, 0);
    }
    */
    
    for(;;){
        MbCnt++;
        /*
        if(0==(MbCnt%80)){
            RegHolding[0].uVal++;
        }
        mbCnt++;
        RegHolding[MbCnt&0x0F].uVal = RegHolding[(MbCnt-1)&0x0F].uVal + RegHolding[(MbCnt-2)&0x0F].uVal;
        
        osDelay(10);
        if(mbyObj.RcvSt == MB_RX_RCVEND)
        {
            mbsimulink(mbyObj.AduBuff[0]);
            mbyObj.SlaveId = mbyObj.AduBuff[0];
            mbSlavePoll(&mbyObj);
            mbsimulink(mbyObj.AduBuff[0]);
        }
        */
        osDelay(2);
        mbSlavePoll(&mbyObj);
        mbSlavePoll(&mbzObj);
    }
}


/*****************************************************************************//*!
* @brief   	modbus simulink.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/  
void mbsimulink(char mbid)
{
    regDevice = 0x5000;
    regVersion = 0x1000;
    switch(mbid)
    {
    case 1: regFunction = 0x81;break;
    case 2: regFunction = 0x83;break;
    case 3: regFunction = 0x82;break;
    case 4: regFunction = 0x85;break;
    case 5: regFunction = 0x89;break;
    case 6: regFunction = 0x88;break;
    case 7: regFunction = 0x83;break;
    case 8: regFunction = 0x83;break;
    default:
        regFunction = 0x81;break;
    }
    
    RegSetTimeHour = 80;
    RegSetTimeMin = 30;
    
    RegTempSv   = 600;        
    RegHumiSv   = 750; 
    RegColSv    = 100; 
    RegSpeedSv  = 1200; 
    
    if(mbCnt%50 == 0)
    {
        if(bStRun)
        {
        if(RegTempPv < RegTempSv)
            RegTempPv++;
        else if( RegTempPv > RegTempSv)
            RegTempPv--;
        } else {
            if(RegTempPv > 200)
                RegTempPv--;
        }
    }
    
    if(mbCnt%100 == 0)
    {
        if(bStRun)
        {
        if(RegHumiPv < RegHumiSv)
            RegHumiPv++;
        else if( RegHumiPv > RegHumiSv)
            RegHumiPv--;
        } else {
            if(RegHumiPv > 400)
                RegHumiPv--;
        }
        
        
        if(bStRun)
        {
        if(RegSpeedPv < RegSpeedSv)
            RegSpeedPv++;
        else if( RegSpeedPv > RegSpeedSv)
            RegSpeedPv--;
        } else {
            if(RegSpeedPv > 0)
                RegSpeedPv--;
        } 
    }
    
    if(RegHolding[0x1000].uVal == 0x033)
    {
        RegHolding[0x1000].uVal = 0;
        bStRun = 0;
    }
    
    if(RegHolding[0x1000].uVal == 0x0CC)
    {
        RegHolding[0x1000].uVal = 0;
        bStRun = 1;
    }
    
    if(bStRun)
    {
        if(uwTick - TickTimeCnt >= 1000)
        {
            TickTimeCnt+=1000;
            RegWorkTimeMin ++;
            if(RegWorkTimeMin >= 60)
            {
                RegWorkTimeMin = 0;
                RegWorkTimeHour ++;
            }
        }
    } else {
        RegWorkTimeMin = 0;
        RegWorkTimeHour = 0;
    }
}
